package ui;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.AbstractButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import model.User;
import service.IUserService;
import service.UserService;
public class UpdateUsers extends JDialog implements ActionListener{
	User user;
	private JLabel label,label1,label2;
	private JPasswordField password1,password2;
	private JButton yes,cancel;
	private JPanel p1;
	JLabel name;
	public UpdateUsers(){
		label=new JLabel(" 用户名 :");
		name=new JLabel();
		label1=new JLabel(" 新密码 ：");
		label2=new JLabel("确定密码：");
		password1=new JPasswordField(20);
		password2=new JPasswordField(20);
		yes=new JButton("确定");
		cancel=new JButton("取消");
		
		p1 = (JPanel)this.getContentPane();
		p1=new JPanel();
		p1.add(label);
		p1.add(name);
		p1.add(label1);p1.add(password1);
		p1.add(label2);p1.add(password2);
		p1.add(yes);p1.add(cancel);

		label.setBounds(80,35,100,30);
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		name.setBounds(180,35,100,30);

		label1.setBounds(80,70,100,30);
		label1.setFont(new Font("宋体", Font.PLAIN, 20));
		password1.setBounds(180, 70, 160, 30);

		label2.setBounds(85,110,100,30);
		label2.setFont(new Font("宋体", Font.PLAIN, 20));
		password2.setBounds(180, 110, 160, 30);

		yes.setBounds(135,170,75,35);
		yes.setFont(new Font("宋体", Font.PLAIN, 20));
		yes.setForeground(Color.BLUE);
		cancel.setBounds(225, 170, 75,35);
		cancel.setFont(new Font("宋体", Font.PLAIN, 20));
		cancel.setForeground(Color.BLUE);

		yes.addActionListener(this);
		cancel.addActionListener(this);

		this.add(p1);
		setTitle("修改密码");
		setBounds(100,100,450, 300);
		getContentPane().setLayout(new BorderLayout());
		p1.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(p1, BorderLayout.CENTER);
		p1.setLayout(null);


		setVisible(true);
		setResizable(false);
	}

/*	public static void main(String[] args) {
		new UpdateUsers();

	}**/
	public void setUser(User user){
		this.user=user;
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==yes){
			String name1 = String.valueOf(name.getText());
			if(String.valueOf(password1.getPassword()).equals(String.valueOf(password2.getPassword()))){
				try {
					IUserService service=new UserService();
					user.setpassword(String.valueOf(password1.getPassword()));
					user.setname(name1);
					if(service.updateUser(user))
						JOptionPane.showMessageDialog(null, "修改成功！");
					setVisible(false);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				}
			else{
				JOptionPane.showMessageDialog(null, "两次输入的密码不一致，请再次确认密码！");
				password2.setText("");
			}	}	
		if(e.getSource()==cancel){
			setVisible(false);
		}
	}
}

